package com.xz.xzutils.module.xzmybatislog;

import com.intellij.execution.ui.ConsoleViewContentType;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Key;
import com.xz.xzutils.components.console.SqlConsole;
import com.xz.xzutils.components.console.SqlConsoleCreator;
import com.xz.xzutils.utils.SqlParser;
import com.xz.xzutils.utils.dto.SqlDTO;
import org.jetbrains.annotations.NotNull;

import java.util.Set;
import javax.swing.*;

/**
 * <p>
 *
 * </p>
 *
 * @author DONG.WANG17@HAND-CHINA.COM
 * @since 2025-01-14 20:08
 */
public class XzMybatisLogManager implements Disposable {
    public static final Key<XzMybatisLogManager> KEY = Key.create(XzMybatisLogManager.class.getName());
    // TODO 通过配置获取
    public static final String SQL_PREFIX = "Preparing:";
    public static final String PARAM_PREFIX = "Parameters:";
    // 字段类型——字符类型需要单引号拼接
    private final Set<String> needSingleQuotes = Set.of("String", "Date", "Time", "LocalDate", "LocalTime", "LocalDateTime", "Timestamp");
    private final Set<String> noQuotes = Set.of("Integer", "Long", "Float", "Double", "BigDecimal", "Boolean");
    // sql 占位符
    private final String placeholder = "?";
    private final Project project;
    private final SqlConsole sqlConsole;
    // 指针，为空，则当前未指向任何 sql，否则指向需要等待解析参数的 sql
    private SqlDTO pointer;

    private XzMybatisLogManager(@NotNull Project project) {
        this.project = project;
        this.sqlConsole = SqlConsoleCreator.createSqlConsole(this.project);
    }

    public static XzMybatisLogManager getInstance(@NotNull Project project) {
        XzMybatisLogManager manager = project.getUserData(KEY);
        if (manager == null) {
            manager = new XzMybatisLogManager(project);
            project.putUserData(KEY, manager);
        }
        return manager;
    }

    public void addLine(@NotNull String line) {
        // 如果是参数行，判断当前指针，如果当前指针为空、或未指向sql、或 sql 不需要参数，则忽略该行
        if (line.contains(PARAM_PREFIX) && (null == this.pointer || null == this.pointer.getTemplateSql() || !this.pointer.getNeedParameters())) {
            return;
        }
        if (null == this.pointer) {
            this.pointer = new SqlDTO();
        }
        // 解析当前行
        SqlParser.parseLine(line, this.pointer);
        if(Boolean.TRUE.equals(this.pointer.getNeedParameters())){
            // 进入下次循环
            return;
        }
        if (Boolean.TRUE.equals(this.pointer.getParseSuccess())) {
            this.sqlConsole.println(this.pointer.getSqlKey(), ConsoleViewContentType.LOG_INFO_OUTPUT);
            this.sqlConsole.println(this.pointer.getSql(), ConsoleViewContentType.LOG_DEBUG_OUTPUT);
        }else{
            this.sqlConsole.println(this.pointer.getSqlKey(), ConsoleViewContentType.LOG_ERROR_OUTPUT);
            this.sqlConsole.println("# SQL语句:\t"+this.pointer.getTemplateSql(), ConsoleViewContentType.LOG_ERROR_OUTPUT);
            this.sqlConsole.println("# SQL参数:\t"+this.pointer.getParameters(), ConsoleViewContentType.LOG_ERROR_OUTPUT);
            this.sqlConsole.println("# 提示信息:\t"+this.pointer.getMessage(), ConsoleViewContentType.LOG_ERROR_OUTPUT);
        }
        // 重置指针
        this.pointer = null;
    }

    public JComponent getConsolePanel() {
        return sqlConsole.getComponent();
    }

    @Override
    public void dispose() {
        sqlConsole.dispose();
    }
}
